﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_QLPhanHoiKH : System.Web.UI.Page
{
    PhanHoiCtr phanHoiCtr = new PhanHoiCtr();
    DanhGiaCtr danhGiaCtr = new DanhGiaCtr();
    public void checkAlreadyLogin()
    {
        if ( Session["UserID"].ToString() == null||Session["QuanTri"].ToString()==null)
            Response.Redirect("Login.aspx");
    }
    private void loadDuLieu()
    {
       // gvPhanHoi.DataSource = phanHoiCtr.LayPhanHoi1();
        //gvPhanHoi.DataBind();
        //gvDanhGia.DataSource = danhGiaCtr.LayDanhGia1();
        //gvDanhGia.DataBind();
        MultiView1.ActiveViewIndex = 0;
        blKQDanhGia.Text = "";
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        checkAlreadyLogin();
        if (!IsPostBack)
        {
            loadDuLieu();
            txtduyet.Attributes.Add("onkeypress", "return clickButton(event,'" + btduyet.ClientID + "')");
        }

    }
    protected void btguiykien_Click(object sender, EventArgs e)
    {
        try
        {
            String email = txtEmail.Text;
            String subject = "Ý Kiến Khách Hàng";
            String body=txtYKien.Text;
            GoogleMail.Send(email,subject,body);
            phanHoiCtr.capNhatPhanHoiThanhdaXuLy(int.Parse(hdmaph.Value));
            Response.Redirect("QLPhanHoiKH.aspx");
        }
        catch
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alert",
               "alert('Lỗi kết nối!');", true);   
        }
    }
    protected void gvPhanHoi_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvPhanHoi.PageIndex = e.NewPageIndex;
        gvPhanHoi.DataSource = phanHoiCtr.LayPhanHoi1();
        gvPhanHoi.DataBind();
    }
    protected void gvPhanHoi_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {


            if (e.CommandName.Equals("Edit"))
            {
                int maph = int.Parse(e.CommandArgument.ToString());
                hdmaph.Value = maph.ToString();
                //string maLoai = gvPhanLoai.SelectedRow.Cells[1].Text;
                EPhanHoi phanHoi = phanHoiCtr.LayPhanHoi(maph.ToString());
               
                txtNoiDung.Text = phanHoi.NoiDung;
                txtEmail.Text = phanHoi.Email;
                MultiView1.ActiveViewIndex = 1;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void gvPhanHoi_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int maph = int.Parse(gvPhanHoi.Rows[e.RowIndex].Cells[0].Text);
        phanHoiCtr.xoaPhanHoi(maph);
        loadDuLieu();

    }
    protected void gvPhanHoi_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btn = (LinkButton)e.Row.Cells[5].Controls[1];
            //khi click vao nut xoa se phat sinh doan ma javascript thong bao xac nhan co muon xoa hay khong
            btn.OnClientClick = "return confirm('Bạn có chắc chắn xóa mã phản hồi :" + e.Row.Cells[0].Text + " này không?')";
        }
    }
    protected void gvDanhGia_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int madg = int.Parse(gvDanhGia.Rows[e.RowIndex].Cells[0].Text);
        danhGiaCtr.DeleteDanhGiaH(madg);
        loadDuLieu();
    }
    protected void gvDanhGia_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton btn = (LinkButton)e.Row.Cells[4].Controls[1];
            //khi click vao nut xoa se phat sinh doan ma javascript thong bao xac nhan co muon xoa hay khong
            btn.OnClientClick = "return confirm('Bạn có chắc chắn xóa mã đánh giá :" + e.Row.Cells[0].Text + " này không?')";
        }
        /*if (e.Row.RowType == DataControlRowType.Header)
        {
            //Find the checkbox control in header and add an attribute
            ((CheckBox)e.Row.FindControl("CheckBox3")).Attributes.Add("onclick", "javascript:SelectAll('" +
                    ((CheckBox)e.Row.FindControl("CheckBox3")).ClientID + "')");
        }*/
    }
    protected void gvDanhGia_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvDanhGia.PageIndex = e.NewPageIndex;
        if (String.IsNullOrEmpty(txtduyet.Text))
        {
            gvDanhGia.DataSource = danhGiaCtr.LayDanhGia1();
        }
        else
        {
            gvDanhGia.DataSource = danhGiaCtr.timKiemDanhGia(txtduyet.Text.Trim());
        }
        gvDanhGia.DataBind();
    }
    
    protected void btduyet_Click(object sender, EventArgs e)
    {
        DataTable kqDanhGia = danhGiaCtr.timKiemDanhGia(txtduyet.Text.Trim());
        if (kqDanhGia.Rows.Count > 0)
        {
            gvDanhGia.DataSource = kqDanhGia;
            gvDanhGia.DataBind();
        }
        else
        {
            blKQDanhGia.Text = "Không có đánh giá nào. ";
        }
    }
    protected void gvPhanHoi_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
}